<?php
		session_start();

		require 'dbconnect.inc.php';
		$id=$_POST['hide'];
		$user=$_SESSION['emaild'];
		$result=mysqli_query($link,"select *from votes where id='$id' and user='$user'") or die($conn_error_msg);
		$count=mysqli_num_rows($result);
		if($count==0){
			mysqli_query($link,"insert into votes(id,user) values('$id','$user')");
		}



		if($_POST['submit']=='up')
		{
			$res=mysqli_query($link,"update votes set votetype=TRUE where id='$id' and (votetype=0 or votetype=5) and user='$user'");
			$effected=mysqli_affected_rows($link);
			
			if($effected)
			{	
				mysqli_query($link,"update content set votes=votes+1 where id='$id'");
			}
			else
			{
				mysqli_close($link);
				header('Location: main.php');
				die();
			}
		}
		elseif($_POST['submit']=='down')
		{
			$res=mysqli_query($link,"update votes set votetype=FALSE where id='$id' and (votetype=1 or votetype=5) and user='$user'");
			$effected=mysqli_affected_rows($link);
			
			if($effected)
			{	
				mysqli_query($link,"update content set votes=votes-1 where id='$id'");
				
			}
			else
			{
				mysqli_close($link);
				header('Location: main.php');
				die();
			}
		}

		mysqli_close($link);
		header('Location: main.php');	
		die();



?>
